package com.wu.foodsafetyplayer.app;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.wu.foodsafetyplayer.R;
import com.wu.foodsafetyplayer.app.bean.DeviceByMomeBean;
import com.wu.foodsafetyplayer.app.bean.MonitorVideoBean;
import com.wu.foodsafetyplayer.app.bean.UserEntity;
import com.wu.foodsafetyplayer.app.fragment.main.MonitorFragment;
import com.wu.foodsafetyplayer.http.CommonOkHttpClient;
import com.wu.foodsafetyplayer.utils.ACache;
import com.wu.foodsafetyplayer.utils.SpUtil;

import org.jetbrains.annotations.Nullable;

import java.io.IOException;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class SplashActivity extends BaseActivity {
    private static final String TAG = SplashActivity.class.getSimpleName();
    private MonitorVideoBean monitorVideoBean;
    private DeviceByMomeBean deviceByMomeBean;
    private String res, resByMome;//设备数据
    private Context context;
    private boolean isOk = false;
    private boolean isOk2 = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launcher);
        context = this;
        initMyData();
        initPublicData();
//        Handler handler = new Handler();
//        handler.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                startActivity();
//            }
//        }, 1000);
    }

    private void startActivity() {
        Log.e(TAG, "startActivity-----------");
        UserEntity userEntity = SpUtil.INSTANCE.getUserInfo();
        if (userEntity.getIsnoLogin()) {
            startActivity(new Intent(this, LoginActivity.class));
            finish();
        } else {
            startActivity(new Intent(this, AppMainActivity.class));
            finish();
        }
    }


    /**
     * 初始化数据
     */
    public void initPublicData() {
        CommonOkHttpClient.FUNC_TASK.execute(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient().newBuilder()
                        .build();
                Request request = new Request.Builder()
                        .url("http://daxun.tpddns.cn:81/live/Query?username=admin&password=admin&treemode=0&name=")
                        .method("GET", null)
                        .addHeader("content-type", "form-data")
                        .addHeader("Cookie", "IDHTTPSESSIONID=r4EN2qalGkkbZUS")
                        .build();
                try {
                    Response response = client.newCall(request).execute();
                    String res2 = response.body().string();
                    Log.e(TAG, "-----initData: " + res2);
                    if (!TextUtils.isEmpty(res2)) {
                        ACache.get(context).put("live_data", res2);
                        monitorVideoBean = new Gson().fromJson("{\"data\": " + res2 + "}", MonitorVideoBean.class);
                        if (monitorVideoBean != null && monitorVideoBean.data != null && !monitorVideoBean.data.isEmpty()) {
//                            handler.sendEmptyMessage(PUBLIC_MSG);
                        }
                    }

                    isOk = true;
                    if (isOk2) {
                        isOk = false;
                        startActivity();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (ClassCastException e) {
                    Log.e(TAG, "开始请求设备数据成功,解析失败");
                } catch (JsonSyntaxException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * 初始化自己添加的设备的数据
     */
    public void initMyData() {
        String paramUsername = ACache.get(context).getAsString("name");
        String paramPassword = ACache.get(context).getAsString("pass");
        Log.e(TAG, "initMyData: paramUsername=" + paramUsername);
        Log.e(TAG, "initMyData: paramPassword=" + paramPassword);
        if (TextUtils.isEmpty(paramUsername) || TextUtils.isEmpty(paramPassword)) {
            isOk2 = true;
            return;
        }
        String paramMemo = paramUsername + paramPassword;
        CommonOkHttpClient.FUNC_TASK.execute(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient().newBuilder()
                        .build();
                Request request = new Request.Builder()
                        .url("http://daxun.tpddns.cn:81/QueryOrgTreeByMemo?username=" + paramUsername + "&password=" + paramPassword + "&memo=" + paramMemo + "&hls=&rtsp=")
                        .method("GET", null)
                        .addHeader("content-type", "form-data")
                        .addHeader("Cookie", "IDHTTPSESSIONID=r4EN2qalGkkbZUS")
                        .build();
                try {
                    Response response = client.newCall(request).execute();
                    String res2 = response.body().string();
                    Log.e(TAG, "-----initData resByMome: " + res2);
                    if (!TextUtils.isEmpty(res2)) {
                        ACache.get(context).put("live_data_by_mome", res2);
                        deviceByMomeBean = new Gson().fromJson(res2, DeviceByMomeBean.class);
                        if (deviceByMomeBean != null && deviceByMomeBean.items != null && !deviceByMomeBean.items.isEmpty()) {
//                            handler.sendEmptyMessage(PUBLIC_MSG);
                        }
                    }

                    isOk2 = true;
                    if (isOk) {
                        isOk2 = false;
                        startActivity();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (ClassCastException e) {
                    Log.e(TAG, "开始请求设备数据成功,解析失败");
                } catch (JsonSyntaxException e) {
                    e.printStackTrace();
                }
            }
        });
    }

//    @Override
//    protected int getLayoutId() {
//        return R.layout.activity_launcher;
//    }
//
//    @Override
//    protected void init(@org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
//        startActivity();
//    }
}
